<!--
 * @Author: hq
 * @Date: 2022-05-20 15:22:39
 * @LastEditors: hq
 * @LastEditTime: 2022-06-16 13:34:28
 * @Description: your project
 * @version: 1.0
-->
<template>
  <div>
    <div class="dialoginfo">
      <el-row>
        <el-col :span="12">
          <el-form :model="form" label-width="80px">
            <el-form-item label="活动名称">
              <el-input v-model="form.typeName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
              <el-input
                oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                maxlength="3"
                v-model="form.sort"
              ></el-input>
            </el-form-item>
            <el-form-item label="图标">
              <el-upload
                class="avatar-uploader"
                action=""
                :show-file-list="false"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.shopStateId">
                <el-option
                  v-for="item in shopState"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div class="row">
        <el-button type="primary" @click="handleSubmit">提交</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        typeName: "",
        sort: "",
        imageUrl: "",
        shopStateId: 1,
      },
      shopState: [
        { id: 1, name: "启用" },
        { id: 2, name: "禁用" },
      ],
    };
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //提交
    handleSubmit() {
      this.$emit("handleClose");
    },
    //取消
    handleCancel() {
      this.$emit("handleClose");
    },
  },
};
</script>

<style lang="scss" scoped>
.dialoginfo {
  padding: 0 0 40px 20px;
  box-sizing: border-box;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
.row {
  padding-left: 80px;
}
</style>
